using System;
using XmlRpcMvc;
using XmlRpcMvc.Common;
using mitasoft.Component.Models.Blog;
using StructureMap;
using mitasoft.Component.Services.Interfaces;
using System.Web.Security;
using System.Collections.Generic;
using System.Web;
using System.IO;
using mitasoft.Web.Core;

namespace mitasoft.Web.XmlRpc
{
    public class MetaWeblogApi : IXmlRpcService
    {

        private IBlogService _blogService;

        public MetaWeblogApi()
        {
            _blogService = ObjectFactory.GetInstance<IBlogService>();
        }

        [XmlRpcMethod("wp.newCategory")]
        public string WlwNewCategory(string blog_id, string username, string password, WlwCategory category)
        {

            if (!Membership.ValidateUser(username, password))
            {
                return null;
            }

            return _blogService.AddCategory(category.Name).Id.ToString();
        }

        [XmlRpcMethod("metaWeblog.newPost")]
        public string NewPost(string blogid, string username, string password, PostInfo post, bool publish)
        {

            if (!Membership.ValidateUser(username, password))
            {
                return string.Empty;
            }

            var newCreatedPost = _blogService.NewPost(post.Title, post.Description,
                post.DateCreated, publish, post.Categories);

            return newCreatedPost.Id.ToString();

        }

        [XmlRpcMethod("metaWeblog.editPost")]
        public bool EditPost(string postid, string username, string password, PostInfo post, bool publish)
        {

            if (!Membership.ValidateUser(username, password))
            {
                return false;
            }

            _blogService.UpdatePost(int.Parse(postid), post.Title, post.Description,
                post.DateCreated, publish, post.Categories);

            return true;
        }

        [XmlRpcMethod("metaWeblog.getPost")]
        public PostInfo GetPost(string postid, string username, string password)
        {

            if (!Membership.ValidateUser(username, password))
            {
                return null;
            }

            return _blogService.GetPost(int.Parse(postid));
        }

        [XmlRpcMethod("metaWeblog.getCategories")]
        public CategoryInfo [] GetCategories(string blogid, string username, string password)
        {

            if (!Membership.ValidateUser(username, password))
            {
                return null;
            }
            return _blogService.GetCategories();
        }

        [XmlRpcMethod("metaWeblog.getRecentPosts")]
        public PostInfo [] GetRecentPosts(string blogid, string username, string password, int numberOfPosts)
        {

            return new PostInfo [] { };
        }

        [XmlRpcMethod("metaWeblog.newMediaObject")]
        public MediaObjectInfo NewMediaObject(string blogid, string username, string password, MediaObject mediaObject)
        {

            if (!Membership.ValidateUser(username, password))
            {
                return null;
            }

            // save on disk
            MediaObjectHelper.SaveMediaObjectFromLiveWriter(mediaObject);

            // return new link
            return new MediaObjectInfo
            {
                Url = UrlHelperExtensions.MediaUrl(mediaObject.Name)
            };
        }

        [XmlRpcMethod("blogger.deletePost")]
        public bool DeletePost(string key, string postid, string username, string password, bool publish)
        {

            return false;
        }

        [XmlRpcMethod("blogger.getUsersBlogs")]
        public BlogInfo [] GetUsersBlogs(string key, string username, string password)
        {

            if (!Membership.ValidateUser(username, password))
            {
                return null;
            }

            return new BlogInfo [] 
            {
                new BlogInfo
                {
                    BlogId = "1",
                    BlogName = "mitasoft blog", 
                    Url = "http://moldovanu.net"
                }
            };

        }

        [XmlRpcMethod("blogger.getUserInfo")]
        public UserInfo GetUserInfo(string key, string username, string password)
        {

            if (!Membership.ValidateUser(username, password))
            {
                return null;
            }

            return new UserInfo
            {
                NickName = "mitasoft",
                FirstName = "Mihai",
                LastName = "Moldovan",
                UserId = "1"
            };
        }
    }
}